using QFramework;
using UnityEngine;
using System;
using System.Collections.Generic;

namespace HareGame
{
    [MonoSingletonPath("GameEntry/SDKManager")]
    public class SDKManager : MonoBehaviour, ISingleton
    {
        private ISDKProxy _sdk;

        private TestConfig _testConfig;

        /// <summary>
        /// 初始化
        /// </summary>
        public void OnSingletonInit()
        {
            _sdk = new TestSDK();
            _testConfig = GameEntry.Config.GetConfig<TestConfig>();
#if UNITY_EDITOR
            _testConfig.AdOpen = false;
#endif
        }

        /// <summary>
        /// 定义单例
        /// </summary>
        public static SDKManager Instance
        {
            get { return MonoSingletonProperty<SDKManager>.Instance; }
        }

        /// <summary>
        /// 初始化SDK
        /// </summary>
        /// <param name="result">结果</param>
        public void Init(Action<bool> result)
        {
            _sdk.Init(result);
        }

        /// <summary>
        /// 清空玩家数据
        /// </summary>
        public void ClearUserData()
        {
            _sdk.ClearUserData();
        }

        /// <summary>
        /// 播放插页广告
        /// </summary>
        public void ShowFullAds()
        {
            LogKit.I($"请求播放插页广告");
            if (!_testConfig.AdOpen)
            {
                LogKit.I("测试模式,跳过插页广告");
                return;
            }
            _sdk.ShowFullAds();
        }

        /// <summary>
        /// 播放激励广告
        /// </summary>
        /// <param name="adsType">广告类型</param>
        /// <param name="rewardAction">能否获得奖励回调</param>
        public void ShowRewardAds(AdsType adsType, Action<bool> result)
        {
            LogKit.I($"请求播放激励广告<color=yellow>{adsType}</color>");
            if (!_testConfig.AdOpen)
            {
                LogKit.I("测试模式,直接发放激励广告奖励");
                result?.Invoke(true);
                return;
            }
            if (!GameEntry.Util.IsNetworkReachability())
            {
                LogKit.I("没网络,无法播放激励广告");
                result?.Invoke(false);
                return;
            }
            _sdk.ShowRewardAds(adsType, result);
        }

        /// <summary>
        /// 前往商店
        /// </summary>
        public void Go2Store()
        {
#if UNITY_ANDROID
            Application.OpenURL("https://play.google.com/store/apps/details?id=" + Application.identifier);
#elif UNITY_IOS
            UnityEngine.iOS.Device.RequestStoreReview();    
#endif
            GameEntry.GameData.SetBool(UserData.HasRate, true);
        }

        /// <summary>
        /// 上报引导事件
        /// </summary>
        /// <param name="stepID"></param>
        public void SendGuideEvent(int stepID)
        {
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("id", stepID.ToString());
            SendEvent("guide", param);
        }

        /// <summary>
        /// 上报一次性埋点事件
        /// </summary>
        public void SendOnceEvent(string eventName, Dictionary<string, object> param = null)
        {
            if (!GameEntry.GameData.HasKey("eventID_" + eventName))
            {
                _sdk.SendEvent(eventName, param);
                GameEntry.GameData.SetBool("eventID_" + eventName, true);
            }
        }

        /// <summary>
        /// 上报埋点事件
        /// </summary>
        /// <param name="eventName">事件名称</param>
        public void SendEvent(string eventName)
        {
            _sdk.SendEvent(eventName);
            LogKit.I("上报埋点:" + eventName);
        }

        /// <summary>
        /// 上报埋点事件
        /// </summary>
        /// <param name="eventName">事件名称</param>
        /// <param name="param">事件参数</param>
        public void SendEvent(string eventName, Dictionary<string, object> param)
        {
            _sdk.SendEvent(eventName, param);

            string msg = "上报埋点:" + eventName;
            if (param != null)
            {
                foreach (var v in param)
                {
                    msg += "\n" + v.Key + "****" + v.Value;
                }
            }
            LogKit.I(msg);
        }
    }


    public enum AdsType
    {
        JL_DailyReward
    }
}